// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.vehicles;

import java.util.HashMap;
import zombie.ai.State;
import zombie.ai.astar.AStarPathFinder;
import zombie.audio.parameters.ParameterZombieState;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoZombie;
import zombie.core.skinnedmodel.advancedanimation.AnimEvent;
import zombie.gameStates.IngameState;
import zombie.iso.IsoChunk;
import zombie.iso.IsoWorld;
import zombie.network.GameServer;
import zombie.network.ServerMap;

public final class PathFindState2 extends State {
    private static final Integer PARAM_TICK_COUNT = 0;

    @Override
    public void enter(IsoGameCharacter owner) {
        HashMap hashMap = owner.getStateMachineParams(this);
        owner.setVariable("bPathfind", true);
        owner.setVariable("bMoving", false);
        ((IsoZombie)owner).networkAI.extraUpdate();
        hashMap.put(PARAM_TICK_COUNT, IngameState.instance.numberTicks);
    }

    @Override
    public void execute(IsoGameCharacter owner) {
        HashMap hashMap = owner.getStateMachineParams(this);
        PathFindBehavior2.BehaviorResult behaviorResult = owner.getPathFindBehavior2().update();
        if (behaviorResult == PathFindBehavior2.BehaviorResult.Failed) {
            owner.setPathFindIndex(-1);
            owner.setVariable("bPathfind", false);
            owner.setVariable("bMoving", false);
        } else if (behaviorResult == PathFindBehavior2.BehaviorResult.Succeeded) {
            int int0 = (int)owner.getPathFindBehavior2().getTargetX();
            int int1 = (int)owner.getPathFindBehavior2().getTargetY();
            IsoChunk isoChunk = GameServer.bServer
                ? ServerMap.instance.getChunk(int0 / 10, int1 / 10)
                : IsoWorld.instance.CurrentCell.getChunkForGridSquare(int0, int1, 0);
            if (isoChunk == null) {
                owner.setVariable("bPathfind", false);
                owner.setVariable("bMoving", true);
            } else {
                owner.setVariable("bPathfind", false);
                owner.setVariable("bMoving", false);
                owner.setPath2(null);
            }
        } else {
            if (owner instanceof IsoZombie) {
                long _long = (Long)hashMap.get(PARAM_TICK_COUNT);
                if (IngameState.instance.numberTicks - _long == 2L) {
                    ((IsoZombie)owner).parameterZombieState.setState(ParameterZombieState.State.Idle);
                }
            }
        }
    }

    @Override
    public void exit(IsoGameCharacter owner) {
        if (owner instanceof IsoZombie) {
            ((IsoZombie)owner).networkAI.extraUpdate();
            ((IsoZombie)owner).AllowRepathDelay = 0.0F;
        }

        owner.setVariable("bPathfind", false);
        owner.setVariable("bMoving", false);
        owner.setVariable("ShouldBeCrawling", false);
        PolygonalMap2.instance.cancelRequest(owner);
        owner.getFinder().progress = AStarPathFinder.PathFindProgress.notrunning;
        owner.setPath2(null);
    }

    @Override
    public void animEvent(IsoGameCharacter owner, AnimEvent event) {
    }

    @Override
    public boolean isMoving(IsoGameCharacter owner) {
        return owner.isMoving();
    }
}
